(function($){
	$.fn.paginate=function(opts){
		var opts=opts||"";
		//前台参数、后台参数、json:key value,当前页、上一页、总页数、后台url、下一页、上一页/下一页是否残废、
		//分页总html、首页html、分页html、跳转html、尾部html、分页参数
		var json={},param={},key,value,pageNow,pagePre,maxPage,url,pageNext,isFirst,isLast,
		pageHtml,indexHtml,contentHtml,goPageHtml,endHtml,pageNowStr;
		var config= {
			viewSize: 10,  //分页导航li的个数
			removeStr: function (str, param) { //移除最后一个"&"
				var lastIndex = str.lastIndexOf(param);
				if (lastIndex > -1) {
					str = str.substring(0, lastIndex) + str.substring(lastIndex + 1, str.length);
					return str;
				}
			}
		}
		var args=$(this).html().toString(); //得到导航栏的HTML
		var array=args.split(";");
		pageNowStr='&pageNow=';
		$.extend(config,opts);
		$(this).html("");	//清空导航栏的HTML
		for(var i in array){
			key=array[i].split("=")[0];
			value=array[i].split("=")[1];			
			if(key!="pageNow"&&key!="maxPage"&&key!="url"){
				param[key]=value;
			}else{
				json[key]=value;
			}
		}
		pageNow=(json.pageNow)?parseInt(json.pageNow):1; //当前页默认为1
		if(pageNow>maxPage||pageNow<=0){ 
			return false;
		}
		maxPage=parseInt(json.maxPage); //总页数
		url=json.url;	//后台url
		var action=json.url+"?";
		var str;
		for(var i in param){
			str=i+"="+param[i]+"&";
			action+=str;
		}
		action=config.removeStr(action,"&");		
		(pageNow==1)&&(isFirst=true);  //当前页为第一页
		(pageNow==maxPage)&&(isLast=true); //当前页最后一页
		(pageNow==maxPage&&maxPage==1)&&(isFirst=isFirst=true); //只有一页
		if(isFirst){ //当前页是第一页
			indexHtml='<ul class="pagination">'+
		    '<li class="disabled"><a><span aria-hidden="true">首页</span></a>'+
		    '</li><li class="disabled"><a aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>';
		}else{
			pagePre=pageNow-1;  //上一页页码
			indexHtml='<ul class="pagination">'+
		    '<li ><a href='+action+pageNowStr+'1'+' ><span aria-hidden="true">首页</span></a>'+
		    '</li><li ><a href='+action+pageNowStr+pagePre+' aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>';
		}
		if(isLast){ //当前页是最后一页
			endHtml='<li class="disabled"><a href=""  aria-label="Next">'+
        	'<span aria-hidden="true">&raquo;</span></a>'+
    		'</li><li class="disabled"><a href="" ><span aria-hidden="true">尾页</span></a></li>';
		}else{
			pageNext=pageNow+1;  //下一页页码
			endHtml='<li><a href='+action+pageNowStr+pageNext+'  aria-label="Next">'+
        	'<span aria-hidden="true">&raquo;</span></a>'+
    		'</li><li><a href='+action+pageNowStr+maxPage+' ><span aria-hidden="true">尾页</span></a></li>';
		}
		if(isFirst&&isLast){ //只有一页
			goPageHtml='</ul>'
		}else{
			goPageHtml=' <li class="pagego"><div>跳转到<input type="type" id="pageInput" placeholder="请输入页码"></div><a id="pageGo">GO</a></li></ul>';
		}
		var length,index;
		if(maxPage<=config.viewSize){ //总页数小于每个页面展示的页数 
			length=maxPage;
			index=1;
		}
		else{ //总页数大于每个页面展示的页数
			length=config.viewSize;
			index=1;
		}
		//当前页大于每个页面展示的页数且页数不够 maxPage=100 pageNow=99 config.viewSize=5
		if(pageNow>=config.viewSize&&pageNow+config.viewSize>=maxPage){ 
			length=maxPage;
			index=maxPage-config.viewSize+1;
		}
		//当前页大于每个页面展示的页数且页数足够多 maxPage=100 pageNow=94 config.viewSize=5
		if(pageNow>=config.viewSize&&pageNow+config.viewSize<maxPage){
			var startPage;
			if(pageNow%config.viewSize==0){
				startPage=Math.floor(pageNow/config.viewSize-1)*config.viewSize+1;
			}else{
				startPage=Math.floor(pageNow/config.viewSize)*config.viewSize+1;
			}
			length=startPage+config.viewSize-1;
			index=startPage;
		}
		contentHtml="";
		var li=""
		for(var p=index;p<length+1;p++){
			li=(p==pageNow)?'<li class="active"><a href='+action+pageNowStr+p+'>'+p+'</a></li>':'<li><a href='+action+pageNowStr+p+'>'+p+'</a></li>';
			contentHtml+=li;
		}
		pageHtml=indexHtml+contentHtml+endHtml+goPageHtml;
		$(this).append(pageHtml);
		/*跳页事件绑定*/
		$(document).on("blur","#pageInput",function(){
			var num = $(this).val().trim();
			/*限制跳页输入正整数*/
			if(num==null||num==''){
				return false;
			}
			else{
				num=parseInt(num);
				if (isNaN(num) || num <= 0 || !(/^\d+$/.test(num))) {
					{
						alert("页码输入不合法!");
						$(this).val("");
						return false;
					}
				}else{
					$(document).on("click","#pageGo",function(){
						var pageIndex=$("#pageInput").val();
						console.log(action+pageNowStr+pageIndex);
						location.href=action+pageNowStr+pageIndex;
					})
				}
			}
		})
	}
})(jQuery);